﻿﻿////// --- 读取`csv`文件
using System;
using System.IO;
using System.Text;
using System.Globalization;
using Newtonsoft.Json;
using CsvHelper;
using CsvHelper.Configuration;


namespace ReadCsv
{
    public class MyClient
    {
        public static void run()
        {
            // string f_path = @"/Users/mac/Desktop/操作记录表.csv";
            // string f_path = @"data/图书表.csv";
            string f_path = @"data/上传路径选项.csv";


            using (FileStream fileStream = new FileStream(f_path, FileMode.Open, FileAccess.Read))
            using (StreamReader reader = new StreamReader(fileStream, Encoding.Default))
            {
                string headerLine = reader.ReadLine();
                string[] headers = headerLine.Split(',');
                BdUtils.log("headers --- ", headers);
            }

            // 读取UTF-8编码的CSV文件
            using (var reader = new StreamReader(f_path, Encoding.UTF8))
            using (var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture) { Encoding = Encoding.UTF8 }))
            {
                // 读取CSV文件中的所有记录
                var records = csv.GetRecords<dynamic>();
                foreach (var record in records)
                {
                    // Console.WriteLine(record);
                    // BdUtils.log(record.A);
                    // BdUtils.log(record.留言ID);

                    // IDictionary<string, object> dictionary = record;
                    // BdUtils.log(dictionary);
                    BdUtils.log("~~~~~~~~");

                    // 将记录转换为JSON字符串
                    var json = JsonConvert.SerializeObject(record);
                    Console.WriteLine(json);
                    // var dc = BdUtils.conv_json_to_dict(json);
                    // BdUtils.log("~~~", dc);
                }
            }
        }
    }
}
